﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FinancialManagement.Models;

namespace FinancialManagement.Utils
{
    public class Util
    {
        public static string DEPFINANCIAL = GetFinancialDepId();
        
        ///2013-10-23修改
        public const string ADMINROLE = "JS001";

        public const string PROJECTTABLEDETAIL = "T_ProjectTableDetails";
        public const string BUDGETAPPLYTABLE = "T_BudgetApplyTable";

        public const string SUCCESS = "success";
        public const string ERROR = "error";


        private static string GetFinancialDepId()
        {
            var id = System.Configuration.ConfigurationManager.AppSettings.Get("DepFinancial");
            if (id != null)
            {
                return id.ToString();
            }
            else
            {
                return "";
            }
        }

        /// <summary>
        /// 获取服务器时间
        /// </summary>
        /// <returns></returns>
        public static DateTime GetServerDate()
        {
            return new Util().GetDateTime();
        }

        private DateTime GetDateTime()
        {
            using (FinancialMGTDataContext dc = new FinancialMGTDataContext())
            {
                return dc.Pro_GetSystemDate().FirstOrDefault().time;
            }
        }

        /// <summary>
        /// 根据表名，使用存储过程生成流水号
        /// </summary>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public static int GetSerialID(string tableName)
        {
            return new Util().GetSID(tableName);
        }

        private int GetSID(string tableName)
        {
            using (FinancialMGTDataContext dc = new FinancialMGTDataContext())
            {
                int? sid = 0;
                dc.Pro_GetSerialIDInt(tableName, ref sid);
                return sid ?? 0;
            }
        }


        public enum RevenueType
        {
            Budget,
            Actual
        }

        public enum DateType
        {
            Start,
            End
        }

        public static bool IsInputDateIncludeOthers(RevenueType type)
        {
            return new Util().IsInputDate(type);
        }

        public bool IsInputDate(RevenueType type)
        {
            var curDate = GetDateTime();
            var startDate = GetInputDate(type, DateType.Start);
            var endDate = GetInputDate(type, DateType.End);

            if (curDate.CompareTo(startDate) >= 0 && curDate.CompareTo(endDate) <= 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private DateTime GetInputDate(RevenueType revenueType, DateType dateType)
        {

            string rev = "";
            string d = "";
            if (revenueType == RevenueType.Budget)
            {
                rev = "Budget";
            }
            else
            {
                rev = "Actual";
            }
            if (dateType == DateType.Start)
            {
                d = "StartDate";
            }
            else
            {
                d = "EndDate";
            }
            string key = rev + d;
            var dateStr = "";
            var date = System.Configuration.ConfigurationManager.AppSettings.Get(key);
            if (date != null)
            {
                dateStr = date.ToString();
                return DateTime.Parse(date);
            }
            else
            {
                return GetServerDate();
            }

        }

        public static int GetAnnualYear(RevenueType type)
        {
            return new Util().GetYear(type);
        }

        private int GetYear(RevenueType type)
        {
            string key = type.ToString() + "Year";

            var year = System.Configuration.ConfigurationManager.AppSettings.Get(key);
            if (year != null)
            {
                return int.Parse(year.ToString());
            }
            else
            {
                return GetServerDate().Year;
            }



        }
    }
}